package top.infopub.mgr.mkt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.SaleRewardAttDao;
import top.infopub.mgr.mkt.dao.SaleRewardDao;
import top.infopub.mgr.mkt.domain.SaleRewardDomain;
import top.infopub.mgr.mkt.service.SaleRewardService;
import top.infopub.mgr.mkt.vo.SaleRewardVo;
import top.infopub.model.mkt.SaleReward;
import top.infopub.model.mkt.SaleRewardAtt;

import java.util.List;

//废弃，使用MarketingReward
@Service
public class SaleRewardServiceImpl extends GenericServiceImpl<SaleReward, String> implements
		SaleRewardService {

	@Autowired
	private SaleRewardDao saleRewardDao;
	
	@Autowired
	private SaleRewardAttDao saleRewardAttDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<SaleReward, String> getDao() {
		return saleRewardDao;
	}

    @Override
    public Page<SaleRewardDomain> selectByDomainWhere(Page<SaleRewardDomain> page,
                                                      SaleRewardVo saleRewardVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<SaleRewardDomain>)saleRewardDao.selectByDomainWhere(saleRewardVo);
    }

    @Override
    public Page<SaleRewardDomain> selectMyCheckByDomainWhere(Page<SaleRewardDomain> page,
                                                             SaleRewardVo saleRewardVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<SaleRewardDomain>)saleRewardDao.selectMyCheckByDomainWhere(saleRewardVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSaleReward(SaleReward saleReward, List<SaleRewardAtt> saleRewardAtts) {
        
        saleRewardDao.insertById(saleReward);
        
        if(CollectionUtils.isNotEmpty(saleRewardAtts)){
            saleRewardAttDao.insertByBatch(saleRewardAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_SALE_REWARD;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, saleReward.getId(), saleReward.getProcessId(), null);
        
    }

    @Override
    public SaleRewardVo queryById(String id) {
        return saleRewardDao.queryById(id);
    }

}